package fibonacci;

import java.util.HashMap;
import java.util.Map;

public class Way2 {
	
	public static void main(String[] args) {
		final int num = 20;
		Way2 way2 = new Way2();
		//结果为：6765，一共执行了39次
		System.out.println("结果为："+way2.fab(num)+"，一共执行了"+way2.count+"次");
	}
	
	private int count = 0;
	
	private Map<Integer,Integer> map = new HashMap<>() {

		private static final long serialVersionUID = 1L;

		{
			put(1,1);
		}
	};
	
	public int fab(int num) {
		count++;
		if(num<=0) {
			return 0;
		} else {
			if(map.get(num)==null) {
				map.put(num,fab(num-1) + fab(num-2));
			}
			return map.get(num);
		}
	}

}
